package org.example.captcha;


import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.CircleCaptcha;
import cn.hutool.captcha.LineCaptcha;
import cn.hutool.captcha.ShearCaptcha;
import cn.hutool.core.lang.Console;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.mock.web.MockHttpSession;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.net.http.HttpResponse;

@RestController
@RequestMapping("test1")
public class test {
    //读取properties的值
    @Value("${proper.pro}")
    private String pro;
    //读取yml的值，yml和yaml是一样的配置文件，只是名字不一样而已，表示的都是同一个东西
    @Value("${test1.yml}")
    private String yml;

    @RequestMapping("/pro")
    public String readPro() {
        System.out.println(pro);
        System.out.println(yml);
        return pro+" "+yml;
    }

    @RequestMapping("getC")
    public void getC(HttpServletRequest request) {
        Cookie[] cookies = request.getCookies();
        for (Cookie cookie : cookies) {
            if (cookie.getName().equals("captcha")) {
                System.out.println(cookie.getValue());
            }
        }
    }

    @RequestMapping("setC")
    public void setC(HttpServletResponse response) {
        Cookie cookie = new Cookie("captcha", "123");
        response.addCookie(cookie);
    }

    //在响应中添加cookie
    public void t1(HttpServletResponse response) {

        Cookie cookie = new Cookie("test", "test");
        response.addCookie(cookie);
    }

    public static void main(String[] args) {
        HttpSession session = new MockHttpSession();
        session.setAttribute("key", "value");
        System.out.println(session.getAttribute("key"));
    }

    public static void main1(String[] args) {
        //定义图形验证码的长和宽
        LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(200, 100);

        //图形验证码写出，可以写出到文件，也可以写出到流
        lineCaptcha.write("d:/line.png");
        System.out.println(lineCaptcha.getCode());
        Console.log(lineCaptcha.getCode());
        System.out.println(lineCaptcha.verify(lineCaptcha.getCode()));
        System.out.println(lineCaptcha.verify("1234"));

        CircleCaptcha circleCaptcha = CaptchaUtil.createCircleCaptcha(200, 100, 4, 1000);
        circleCaptcha.write("d:/line.png");
        Console.log(circleCaptcha.getCode());

        ShearCaptcha shearCaptcha = CaptchaUtil.createShearCaptcha(200, 100, 4, 10);
        shearCaptcha.write("d:/shear.png");
        Console.log(shearCaptcha.getCode());
    }
}

@ConfigurationProperties(prefix = "text1")
@Configuration
@Data
//@Getter@Setter 会错误。。。。
class text1 {

    private String t1;
    private String t2;


}


